package com.miplus.ccrm.workflow.controller;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.PageData;
import com.miplus.ccrm.common.R;
import com.miplus.ccrm.workflow.model.dto.market.*;
import com.miplus.ccrm.workflow.model.vo.MarketWorkflowVO;
import com.miplus.ccrm.workflow.service.WorkflowMarketService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 话术市场Controller
 */
@Slf4j
@RestController
@RequestMapping("/api/workflow/market")
@RequiredArgsConstructor
@Api(tags = "话术市场管理(暂时就一个公共市场)")
public class WorkflowMarketController {

    private final WorkflowMarketService workflowMarketService;


    @PostMapping("/list")
    @ApiOperation("分页查询市场话术")
    public R<PageData<MarketWorkflowVO>> getMarketPage(@RequestBody MarketQueryDTO dto) {
        return R.SuccessPage( workflowMarketService.getMarketPage(dto));
    }


    @PostMapping("/manage")
    @ApiOperation("管理市场话术（上架/下架）")
    public R<CmdResVo> manageMarketWorkflow(@RequestBody ShelfToMarketDTO dto,
                                            HttpServletRequest request) {
        String userId = getUserId(request);
        CmdResVo result = workflowMarketService.manageMarketWorkflow(dto, userId);
        return R.Success(result);
    }

    /**
     * 从请求中获取用户ID
     */
    private String getUserId(HttpServletRequest request) {
        // 这里应该从token或session中获取用户ID
        // 暂时返回固定值，实际项目中需要根据具体的认证方式实现
        return "current_user_id";
    }
}